package org.springcloud.wisdom.springcloud_03_freshman.admin.service;

import org.springcloud.wisdom.springcloud_03_freshman.admin.dao.NoticeDao;
import org.springcloud.wisdom.springcloud_03_freshman.admin.entity.Notice;
import org.springcloud.wisdom.springcloud_03_freshman.common.bean.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class NoticeService {
    @Autowired
    NoticeDao noticeDao;

    //审核同意
    public boolean approveNotice(Notice notice){
        if (notice==null){
            return false;
        }
        notice.setIspublic("同意");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTimeString = sdf.format(new Date());
        notice.setPublishdt(dateTimeString);
        return noticeDao.updateNotice(notice)>0;
    }
    //审核拒绝
    public boolean rejectNotice(Notice notice){
        if (notice==null){
            return false;
        }
        notice.setIspublic("拒绝");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTimeString = sdf.format(new Date());
        notice.setPublishdt(dateTimeString);
        return noticeDao.updateNotice(notice)>0;
    }
    //发布公告
    public boolean addNotice(Notice notice){
        if (notice == null){
            return false;
        }
        if (notice.getTitle().trim().length() ==0 || notice.getTitle().trim()==null){
            return false;
        }
        if (notice.getContent().trim().length() ==0 || notice.getContent().trim()==null){
            return false;
        }
        if (notice.getIdentity()!=null && notice.getIdentity().equals("admin")){
            notice.setIspublic("同意");
        }
        notice.setIspublic("待审核");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-hh");
        String dataTimeString=sdf.format(new Date());
        notice.setPublishdt(dataTimeString);
        return noticeDao.addNotice(notice)>0;
    }
    //删除公告
    public boolean deleteNotice(Integer id){
        if (id == null || id<0 ){
            return false;
        }
        return noticeDao.deleteNotice(id)>0;
    }
    //查看公告(所有角色)
    public Page<Notice> getNoticePage(int pageNum, int pageSize,String ispublic,Integer type,Integer orderBy){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Notice> list=noticeDao.getNotices(start, pageSize, ispublic, type,orderBy);
        //获取总条目数
        int total=noticeDao.countNotice(ispublic,type);
        //计算总页数
        int pages =(total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Notice> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }
    //批量删除公告
    public boolean delNoticeIds(List<Integer> ids) {
        if (null==ids ||ids.size()==0){
            return false;
        }
        return noticeDao.delbyIds(ids)>0;
    }
    //查看一条公告
    public Notice getNotice(Integer id){
        if (id == null || id<0){
            return null;
        }
        return noticeDao.getNoticeById(id);
    }
}
